package com.kaifamiao.define;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

/*
    <servlet>
        <servlet-name>dispatch</servlet-name>
        <servlet-class>com.kaifamiao.define.DispatchServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>primitive</servlet-name>
        <url-pattern>/dispatch</url-pattern>
    </servlet-mapping>
 */

@WebServlet( name = "dispatch" , urlPatterns = { "/dispatch/*" , "/third/*" } )
public class DispatchServlet extends HttpServlet {

    private final Logger logger = Logger.getLogger( this.getClass().getName() );

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        logger.info( "GET" );
        // 在浏览器地址栏中直接输入URL后访问Servlet属于 GET方式请求
        // 在页面上点击超链接输入 GET 方式请求
        // 以 GET 方式提交表单属于 GET 方式请求

        resp.setContentType( "text/html; charset=UTF-8" );

        // 获得可以向客户端发送数据的字符打印流
        PrintWriter out = resp.getWriter();
        out.println( "<h3>GET</h3>" );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        logger.info( "POST" );
        resp.setContentType( "text/html; charset=UTF-8" );
        PrintWriter out = resp.getWriter();
        out.println( "<h3>POST</h3>" );
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        logger.info( "PUT" );
        resp.setContentType( "text/html; charset=UTF-8" );
        PrintWriter out = resp.getWriter();
        out.println( "<h3>PUT</h3>" );
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        logger.info( "DELETE" );
        resp.setContentType( "text/html; charset=UTF-8" );
        PrintWriter out = resp.getWriter();
        out.println( "<h3>DELETE</h3>" );
    }
}
